package view;

import java.awt.AlphaComposite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.Timer;

import battleModel.LanModel;
import battleModel.Tank;

 
/**
 * A main menu intended to make navigating the game user friendly.
 * There are three different options and choice to exit the game. 
 * The options are listed like so:
 * 
 * Campaign - An option to start a new campaign mode and should take you to 
 * a load screen. You are given a choice to start a new campaign (not saved automatically),
 * loading a previously saved game with the list of saved games given, or going back to 
 * the main menu.
 * 
 * Player v AI - This option is essentially a quick battle. It should take you to screen where
 * you choose a map type, AI difficulty, and a tank color. 
 * 
 * Lan Battle - An option to start a battle over a Lan Network. It will connect to the server
 * and will allow you to decide to host a game, join a game, or go back to the main menu. It 
 * will also display a list of games being hosted and will be identified by names people
 * have entered to host the game.
 * 
 * Exit - Simply quits the game. 
 * 
 * @author Kyle Bush
 * 
 */
@SuppressWarnings("serial")
public class MenuView extends MasterViewPanel {

	private BufferedImage img;
	private GameButton campaign, ai, lan, exit;
	
	private float alphaF, alphaO, alpha;
	private boolean fade;
	private Timer myTimer;

	private String background = "images/backgrounds/HomeScreen.png";

	/**
	 * Creates a new menu view that extends the MasterViewPanel
	 * 
	 * @param m - the main frame to be used
	 * 
	 * @see MasterViewPanel
	 */
	public MenuView(MasterView m) {
		super(m);
		setUpLayout();
		setUpButtons();
		alphaF = 1.0f;
		startMenuFade();
		drawBackGround();
	}

	private void setUpLayout() {
		this.setLayout(null);
	}

	private void setUpButtons() {
		// Campaign Button
		campaign = new GameButton(m.getIcon("cam"), m.getIcon("camS"));
		campaign.setMenuButton();
		campaign.setListener(new CampaignListener());
		this.add(campaign);

		// Player vs. AI button
		ai = new GameButton(m.getIcon("AI"), m.getIcon("AIS"));
		ai.setMenuButton();
		ai.setListener(new StartGameListener());
		this.add(ai);

		// LAN Button
		lan = new GameButton(m.getIcon("lan"), m.getIcon("lanS"));
		lan.setMenuButton();
		lan.setListener(new LanListener());
		this.add(lan);

		// Exit Button
		exit = new GameButton(m.getIcon("exit"), m.getIcon("exitS"));
		exit.setMenuButton();
		exit.setListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		this.add(exit);

		// Button Positioning
		for (int i = 0; i < this.getComponentCount(); i++) {
			this.getComponent(i).setLocation(512, 318 + (69 * i));
		}
	}

	private void drawBackGround() {

		try {
			img = ImageIO.read(new File(background));
		} catch (IOException e) {
			e.printStackTrace();
		}
		repaint();
	}

	/**
	 * Paints the background to the menu.
	 */
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2d = (Graphics2D) g;

		alpha = (alphaO < 1.0) ? alphaO : alphaF;
		
		//Opacity of panel
		g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,alpha));
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
		g.drawImage(img, 0, 0, this);

		alphaO += 0.03f;
		if (alphaO >= 1.0f) {
			alphaO = 1.0f;
		} else {
			repaint();
		}

		try {
			Thread.sleep(5);
		} catch (InterruptedException e){}
	}
	
	private void startMenuFade(){
		alphaO = 0.2f;
		alphaF = 1.0f;
		fade = true;

		myTimer= new javax.swing.Timer(35,new ActionListener() {
			public void actionPerformed (ActionEvent evt) { 
				fadeInOut();
			} 
		});
		
		myTimer.start();
	}
	
	private void fadeInOut(){
		if(alphaF <= 1.0 && !fade){
			alphaF+=0.005f;
			if(alphaF >= 1.0){
				alphaF = 1.0f;
				fade = true;
			}
			//System.out.println("Not Fading");
		} else if(alphaF > .6 && fade){
			alphaF-=0.005f;
			if(alphaF <= .6)
				fade = false;
			//System.out.println("Fading");
		}
		repaint();
	}
	

	private class CampaignListener implements ActionListener {

		public void actionPerformed(ActionEvent arg0) {
			myTimer.stop();
			m.changeView(Views.LOAD, null);
		}
	}

	private class StartGameListener implements ActionListener {

		public void actionPerformed(ActionEvent arg0) {
			myTimer.stop();
			m.changeView(Views.BATTLE, new Tank());
		}
	}
	
	private class LanListener implements ActionListener {

		public void actionPerformed(ActionEvent arg0) {
			myTimer.stop();
			m.changeView(Views.LAN, new LanModel());
		}
	}

	/**
	 * String representation of panel name.
	 */
	public String toString() {
		return "MENU";
	}

}
